/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */
import java.util.*;
public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;   
    private Thing thing;
    private String name; 
    //private Map<String,Thing> map;
    
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, car, school, roof, logcabin, grass, basement, carbarn, box, bridge, end;
      
        // create the rooms
        outside = new Room("马路上。 路上危险容易被敌人发现，赶紧去一个安全的地方吧！");
        car = new Room("车里。车快要爆炸了，看看四周有什么安全的地方吧，记得寻找信号枪哦！");
        school = new Room("学校。这个地方看起来很安全，找找物资保护自己吧。", new Thing("二级头", 30));
        roof = new Room("天台。咦，前面小木屋里好像还有个人，他手里好像拿着东西，偷偷走到他身后看一看吧");
        logcabin = new Room("木屋。经过一番殊死搏斗你终于打败了他，有脚步声，快躲起来吧", new Thing("一级包", 10));
        grass = new Room("草丛。敌人从你的身边路过，好像没有发现你。", new Thing("信号枪", 50));
        box = new Room("集装箱。前面桥上好像有人在交战", new Thing("手枪", 40));
        bridge = new Room("桥上。他们在堵桥，注意观察四周", new Thing("一瓶不明液体", -80));
        basement = new Room("地下室。周围一片漆黑，这种环境容易被敌人从后面偷袭，还是马上离开吧。");
        carbarn = new Room("车库。这里好像有人来过", new Thing("三级甲", 40));
        end = new Room("出口。运气不错嘛，竟然被你找到了出口，赶紧召唤超级空投去取得胜利吧！！！哈哈哈嗝~");
        
        // initialise room exits
        //Room north, Room east, Room south, Room west ,Room up, Room down
        outside.setExit("east", null);
        outside.setExit("south", car);
        outside.setExit("west", null);
        car.setExit("south", null);
        car.setExit("west", school);
        school.setExit("west", roof);
        school.setExit("south", carbarn);
        school.setExit("east", car);
        roof.setExit("west", null);
        roof.setExit("south", logcabin);
        roof.setExit("east", school);
        logcabin.setExit("east", grass);
        logcabin.setExit("north", roof);
        logcabin.setExit("south", end);
        grass.setExit("west", logcabin);
        grass.setExit("east", basement);
        grass.setExit("south", box);
        box.setExit("north", grass);
        box.setExit("east", bridge);
        bridge.setExit("west", box);
        basement.setExit("north", carbarn);
        basement.setExit("west", grass);
        carbarn.setExit("south", basement);
        carbarn.setExit("north", school); 
        // outside.setExits(null, theater, lab, pub,null,null);
        // theater.setExits(null, null, null, outside,uptheater,downtheater);
        // pub.setExits(null, outside, null, null,null,null);
        // lab.setExits(outside, office, null, null,null,null);
        // office.setExits(null, null, null, lab,null,null);
        // uptheater.setExits(null, null, null, null,null,theater);
        // downtheater.setExits(null, null, null, null,theater,null);

        currentRoom = outside;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("拜拜，记得回来看我~");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
         System.out.println();
        System.out.println("欢迎来到9.9包邮版和平不精英!");
        System.out.println("你在降落的途中不小心误入了一个小迷宫");
        System.out.println("你的任务是拿着信号枪，找到出口，只有这样你才能进入精英们的世界。");
        System.out.println("注意：每次移动都会消耗生命值，不要随便乱跑");
        System.out.println("发动你的小脑筋，准备开始吧！");
        System.out.println();
        System.out.println("遇到问题可以键入“help”，但我不一定会帮你哦。");
        System.out.println();
        System.out.println("你现在" + currentRoom.getDescription());
        currentRoom.printExists();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("不知道你在说啥，可以在说一遍吗");
            return false;
        }

        Word commandWord = command.getCommandWord();
        switch(commandWord) {
           case HELP:
               printHelp();
               break;
           case GO:
               goRoom(command);
               break;
           case PICK:
               pick();
               break;
           case CHECK:
               check();
               break;
           case EAT:
               eat(command);
           case LOOK:
               look();
               break;
           case QUIT:
               wantToQuit = quit(command);
               break;
        }
        return wantToQuit;
    }
    
    public void eat(Command command){
       String name = command.getSecondWord();
       Thing thing = player.bag.get(name);
       player.eat(name);
       System.out.println("你装备了："+ thing  );
       System.out.println("你当前生命值为" + player.getStrenght()  );
       player.bag.remove(name);
    }
    
    public void check(){
         if(player.bag.values() == null){
            System.out.println("啊偶，这个背包有点空哦，快去找找物资吧");
         }
         else{
            System.out.print("你的小背包里有:"); 
            for(Thing value : player.bag.values()){
               System.out.println(" "+value);
            }
         }  
    }
    
    public void pick(){
         Thing thing = currentRoom.getTing();
             if(thing != null){
                 player.pick(thing);
                 currentRoom.removeThing();
                 System.out.println("捡到了一个" + thing + "。");
             }
    }
    
    public void look(){
       Thing thing = currentRoom.getTing();
            if(thing != null){
               System.out.println("地上有一个物品是：" + thing + "。");
            }
            else{
               System.out.println("太可惜了，这里啥也没有liao，去别的地方看看吧");
            }
    }
    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("你可以选择的有：");
        System.out.println("go check quit look pick eat");
        System.out.println("go:世界这么大，你想去哪看看");
        System.out.println("check:查看背包物品");
        System.out.println("quit:退出游戏");
        System.out.println("look:观察四周");
        System.out.println("pick:塞到包里");
        System.out.println("eat:穿戴");
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪？");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goNext(direction);
        
        if (nextRoom == null) {
            player.run();
            System.out.println("前方有危险");
        }
        else {
            
            currentRoom = nextRoom;
            player.run();
            System.out.println("你来到了" + currentRoom.getDescription());
            System.out.println("你当前生命值为" + player.getStrenght()  );
            currentRoom.printExists();
        }
    }

    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("退出？");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}